export function randomChinese(num: number): string {
  let result = ''
  for (let i = 0; i < num; i++) {
    const charCode = Math.round(Math.random() * 20901) + 19968
    result += String.fromCharCode(charCode)
  }
  return result
}

export function timeSleep(e: number): Promise<void> {
  return new Promise((ok) => {
    setTimeout(() => {
      ok()
    }, e)
  })
}

/**
 *
 * @param binary 二进制字符串
 * @returns 十进制数字
 */
export function binaryToDecimal(binary: string) {
  let decimal = 0
  let base = 1
  for (let i = binary.length - 1; i >= 0; i--) {
    const digit = Number(binary[i])
    decimal += digit * base
    base *= 2
  }
  return decimal
}

export function generateRandomString(length: number) {
  const characters =
    'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
  let result = ''
  const charactersLength = characters.length
  for (let i = 0; i < length; i++)
    result += characters.charAt(Math.floor(Math.random() * charactersLength))
  return result
}
